package com.o2opuzi.pub.controller

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.o2opuzi.lib.db.ExcludeColumns
import com.o2opuzi.lib.db.Terms
import com.o2opuzi.lib.util.FieldErrorUtils;
import com.o2opuzi.lib.util.PasswordUtils;
import com.o2opuzi.pub.dao.RoleDao;
import com.o2opuzi.pub.dao.UserDao;
import com.o2opuzi.pub.dao.UserOperLogDao;
import com.o2opuzi.pub.model.Role;
import com.o2opuzi.pub.model.User;
import com.o2opuzi.pub.model.UserOptLog
import com.o2opuzi.pub.service.UserService;
import com.o2opuzi.pub.vo.UserCreateVo

@RestController
@RequestMapping("/admin/account")
class AccountController {
	
	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UserOperLogDao userOperLogDao;
	@Autowired
	private UserService userService;
	
	
	@GetMapping("/create")
	def createView(){
		return [
			code:1,
			data:roleDao.findList()
		];
	}
	
	/**
	 * 新增
	 * @auther Y.hj
	 * @param user
	 * @param result
	 * @return
	 */
	@PostMapping("/create")
	def create(@RequestBody @Valid UserCreateVo userVo,BindingResult result){
		if (result.hasErrors()) {
			return [
				code:0,
				messages:FieldErrorUtils.getErrors(result)
			];
		}
		if(userVo.account.length() <4 || userVo.account.length() > 16){
			return [
				code:0,
				messages:[text:"账号的长度在4-16位之间"]
			];
		}
		if(userVo.password.length() <6 || userVo.password.length() > 16){
			return [
				code:0,
				messages:[text:"密码的长度在6-16位之间"]
			];
		}
		if(userVo.password != userVo.confirm_password){
			return [
				code:0,
				messages:[text:"2次输入的密码不一致"]
			];
		}
		if(userDao.exists("account", userVo.account)){
			return [
				code:0,
				messages:[text:"该账号已存在"]
			];
		}
		
		userDao.save([
			account:userVo.account,
			password:PasswordUtils.encrypt(userVo.password),
			role_id:userVo.role_id,
			is_admin:1,
			status:userVo.status,
			createdate:new Date()
		]);
		
		return [
			code:1,
			messages:[text:"新增成功"]
		];
		
	}
	
	/**
	 * 删除
	 * @auther Y.hj
	 * @param user
	 * @return
	 */
	@PostMapping("/delete")
	def delete(@RequestBody User user){
		
		if(user.id == null){
			return  [
				code:0,
				messages:[text:"账号不存在"]
			];
		}
		int ret = userDao.deleteById(user.id);
		if(ret > 0){
			return  [
				code:1,
				messages:[text:"删除成功"]
			];
		}else{
			return  [
				code:0,
				messages:[text:"账号不存在或已删除"]
			];
		}
	}
	
	
	/**
	 * 修改
	 * @auther Y.hj
	 * @param user
	 * @return
	 */
	@PostMapping("/update")
	def update(@RequestBody Map<String, Object> user){
		
		if(StringUtils.isEmpty(user["id"])){
			return [
				code:0,
				messages:[text:"id不能为空"]
			];
		}
		if(StringUtils.isEmpty(user["role_id"])){
			return [
				code:0,
				messages:[text:"请选择角色"]
			];
		}
		if(StringUtils.isEmpty(user["account"])){
			return [
				code:0,
				messages:[text:"账号不能为空"]
			];
		}
		if(((String)user["account"]).length() <4 || ((String)user["account"]).length() > 16){
			return [
				code:0,
				messages:[text:"账号的长度在4-16位之间"]
			];
		}
		if(userDao.find(new Terms(["account","=",user["account"]],["id","<>",user["id"]])) != null){
			return [
				code:0,
				messages:[text:"账号已存在"]
			];
		}
		Map updateModel = [
			account:user["account"],
			status:user["status"],
			role_id:user["role_id"]
		];
		if(!StringUtils.isEmpty(user["password"])){
			if(StringUtils.isEmpty(user["password"])){
				return [
					code:0,
					messages:[text:"密码不能为空"]
				];
			}
			if(user["password"] != user["confirm_password"]){
				return [
					code:0,
					messages:[text:"2次输入的密码不一致"]
				];
			}
			if(((String)user["password"]).length() <6 || ((String)user["password"]).length() > 16){
				return [
					code:0,
					messages:[text:"密码的长度在6-16位之间"]
				];
			}
			updateModel["password"] = PasswordUtils.encrypt(user["password"].toString());
		}
		Integer ret =  userDao.update(updateModel,user["id"]);
		if(ret > 0){
			return [
				code:1,
				messages:[text:"修改成功"]
			];
		}
		return [
			code:0,
			messages:[text:"修改失败"]
		];	
	}
	
	@GetMapping("/update")
	def update(Integer id){
		User user =  userDao.find(new User().excludeCols(["password"]).where().eq("is_admin", 1).and().eq("id", id));
		return [
			code:1,
			data:[
				user:user,
				roleList:roleDao.findList()
			]
		];
	}
	
	@GetMapping("/get/all")
	def getAllAccount(){
		return [
			code:1,
			data:userDao.findListForMap(
				new User().as("u")
				.cols(
					["u.id","u.account","r.name as role_name","u.role_id","u.status","u.createdate"]
				 )
				.leftJoin(Role.class, "r")
				.on("u.role_id", "r.id")
				.where()
				.eq("u.is_admin", 1)
			)
		];
	}
	
	
	/**
	 * 用户操作记录
	 * @auther Y.hj
	 * @param id
	 * @param page
	 * @param size
	 * @return
	 */
	@GetMapping("/optlogs")
	def optlogs(Integer id,Integer page,Integer size){
		if(id == null){
			return [code:0,data:[]];
		}
		return [
			code:1,
			data:new UserOptLog().where().eq("user_id", id).orderByDesc("optdate").selectPage(page,size)
		];
	}
	 
	
	/**
	 * 登录记录
	 * @auther Y.hj
	 * @param id
	 * @param page
	 * @param size
	 * @return
	 */
	@GetMapping("/loginlogs")
	def loginlogs(Integer id,Integer page,Integer size){
		return [
			code:1,
			data:userService.getUserLoginLogs(id, page, size)
		];
	}
	
}
